package com.ly.mpayment.persist.service.downorderrefund;

import java.io.Serializable;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.ly.mpayment.persist.entity.DownOrderRefund;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;

@Service
public class DownOrderRefundServiceImpl extends AbstractService<DownOrderRefund, Serializable> implements IDownOrderRefundService{

	@Override
	public boolean addDownorderrefund(DownOrderRefund fund) {
		if(fund != null){
			this.saveOrUpdate(fund);
			return true;
		}
		return false;
	}

	@Override
	public boolean updateDownorderrefund(DownOrderRefund fund) {
		if(fund != null){
			this.saveOrUpdate(fund);
			return true;
		}
		return false;
	}

	@Override
	public DownOrderRefund findById(String id) {
		return this.findUnique("select d from DownOrderRefund d where d.downrefunduuid = ?", id);
	}

	@Override
	public Page<DownOrderRefund> findPageDownorderrefund(Map<String, Object> parameters, int start, int pageSize) {
		String hql = "select d from DownOrderRefund d left join fetch d.user left join fetch d.downRechargeFlow f where  d.downrefundstatus = 1 #[and d.user.useruuid = :useruuid ]#  #[and f.dowonphonenum = :dowonphonenum ]# ";
		if (parameters.containsKey(":starttime")) {
			hql += " and d.downrefundtime >='" + parameters.get(":starttime").toString().concat(" 00:00:00'");
			parameters.remove(":starttime");
		}
		if (parameters.containsKey(":endtime")) {
			hql += " and d.downrefundtime <='" + parameters.get(":endtime").toString().concat(" 23:59:59'");
			parameters.remove(":endtime");
		}
		hql += "  order by d.downrefundtime desc";
		return this.findPageByNamed(hql, parameters, start, pageSize);
	}

	@Override
	public DownOrderRefund findByDownOrderId(String downOrderId) {
		return this.findUnique("select d from DownOrderRefund d where d.downRechargeFlow.downorderuuid = ?", downOrderId);
	}


}
